
function allchoose() {
    $('.choose').attr('class', 'choose');

}
function close() {
    $('.rights').attr('style', 'display: none')
}
function movies() {
    location.href = '../html/pay.html';
}


function allLog() {
    $('.log').attr('class', 'log');
}

$.ajax({
    url: 'https://www.fastmock.site/mock/bb4157f45a0b5ffdcb3f6d984517a6c0/woniuMovie/getOrders',
    success(data) {
        let orders = data.orders;
        $.ajax({
            url: 'https://www.fastmock.site/mock/bb4157f45a0b5ffdcb3f6d984517a6c0/woniuMovie/getAllOperas',
            success(data) {
                let operas = data.operas;
                function opera(id) {
                    return operas.find(opera => id == opera.id);
                }
                $.ajax({
                    url: 'https://www.fastmock.site/mock/bb4157f45a0b5ffdcb3f6d984517a6c0/woniuMovie/getAllMovies',
                    success(data) {
                        let movies = data.movies;
                        function movie(id) {
                            return movies.find(movie => id == movie.id);
                        }
                        let order = orders.map(item => {
                            return `<div class="myorder">
                                <div class="time">
                                    <span>${item.buytime}</span>
                                    <span>蜗牛订单号：${item.orderid}</span>
                                    <button class="delate"><img src="../images/个人中心/icon02.png" alt=""></button>
                                </div>
                                <div class="news">
                                    <div class="movie">
                                        <img src="${movie(item.movieid).imgSrc}" alt="">
                                    </div>
                                    <div class="introduce">
                                        <ul>
                                            <li class="movieName">
                                                <span>${movie(item.movieid).title}</span>
                                            </li>
                                            <li class="address">
                                                <span>${opera(item.cinemaid).name}</span>
                                            </li>
                                            <li class="seat">
                                                <span>${item.cinemaid}号厅</span>
                                                <span>${item.seats}</span>
                                            </li>
                                            <li class="movieTime">
                                                <span>${item.playtime}</span>
                                            </li>
                                        </ul>
                                    </div>
                                    <span class="price">￥34</span>
                                    <span class="paying">待支付</span>
                                    <div class="pay">
                                        <button class="fukuan"><a href="pay.html">付款</a></button>
                                        <p>查看详情</p>
                                    </div>
                                </div>
                            </div>`
                        });
                        $(".Order").html(order);
                        render();
                    }
                });
            }
        });
    }
});


function render() {
    let loginUser = JSON.parse(localStorage.getItem("loginUser"));
    let chooseSeat = localStorage.getItem("chooseSeat");
    let orders = loginUser.order.map(order => {
        return `<div class="myorder">
                    <div class="time">
                        <span>${order.dateTime}</span>
                        <span>蜗牛订单号：${order.orderId}</span>
                        <button class="delate"><img src="../images/个人中心/icon02.png" alt=""></button>
                    </div>
                    <div class="news">
                    <div class="movie">
                        <img src="${order.movieImgSrc}" alt="">
                    </div>
                        <div class="introduce">
                            <ul>
                                <li class="movieName">
                                    <span>${order.movie}</span>
                                </li>
                                <li class="address">
                                    <span>${order.opera}</span>
                                </li>
                                <li class="seat">
                                    <span>${order.movieClass}</span>
                                    <span>4排1座</span>
                                </li>
                                <li class="movieTime">
                                    <span>今天 ${order.month}月${order.date}日 ${order.time}</span>
                                </li>
                            </ul>
                        </div>
                        <span class="price">￥${order.price}</span>
                        <span class="paying">待支付</span>
                        <div class="pay">
                            <button class="fukuan"><a href="pay.html?id=${order.orderId}">付款</a></button>
                            <p>查看详情</p>
                        </div>
                    </div>
                </div>`
    });
    $(".Order").html(orders);
}
$(function () {

    $(".Order").on('click', '.myorder .time .delate', function () {
        $(this).parent().parent().remove();
    });

    $('.choose').on('click', function () {
        var $get = $('.choose');
        allchoose();
        $(this).attr('class', 'choose on');
        close();
        if ($(this).html() == $get.eq(0).html()) {
            $('.one').attr('style', ' display: block');
        } else if ($(this).html() == $get.eq(1).html()) {
            $('.two').attr('style', ' display: block');
        } else if ($(this).html() == $get.eq(2).html()) {
            $('.three').attr('style', ' display: block');
        }
    });

    //获取用户相关信息
    var users = localStorage.getItem("users");
    var loginUser = localStorage.getItem('loginUser');
    var users = JSON.parse(users);
    var loginUser = JSON.parse(loginUser);
    var changeUser = users.find(user => user.user == loginUser.user)
    if (changeUser.name == undefined) {
        changeUser.name = ''
    } else {
        $('#userName').val(changeUser.name)
    }
    if (changeUser.gender == undefined) {
        $('#nan').attr("checked", "checked");
    } else {
        if (changeUser.gender == "男") {
            $('#nan').attr("checked", "checked");
        } else {
            $('#nv').attr("checked", "checked");
        }
    }
    //获取用户生辰
    var newyear, month, day;
    $('#year').on('change', function () {
        newyear = $('#year option:selected').val();
    })
    $('#month').on('change', function () {
        month = $('#month option:selected').val();
    })
    $('#day').on('change', function () {
        day = $('#day option:selected').val();
    })
    //生活状态
    var life;
    $('#life').on('click', 'input', function () {
        life = $(this).val();
    })
    //行业
    var work
    $('#work').on('change', function () {
        work = $('#work option:selected').val();
    })
    //行业类型
    $('#type').on('change', function () {
        type = $('#type option:selected').val();
    })
    $('#userfilxed').on('click', function () {
        users.map(user => {
            if (user.user == loginUser.user) {
                user.name = $('#userName').val();
                user.gender = $('#sex input:checked').val();
                user.life = life;
                user.birthday = `${newyear}-${month}-${day}`;
                user.work = `${work}`;
                user.type = `${type}`;
                user.personalitywhite = $('#personality').val();
            }
        });
        localStorage.setItem('users', JSON.stringify(users));
        alert('修改成功');
    })
    //修改密码
    $('#savaPass').on('click', function () {
        var userPass = loginUser.pass;
        var oldpass = $('#old').val();
        var newpas = $('#new').val();
        var readpass = $('#readNew').val();
        console.log(userPass, oldpass, newpas, readpass);
        if (oldpass == userPass) {
            if (newpas == readpass) {
                users.map(user => {
                    if (user.pass == loginUser.pass) {
                        user.pass = newpas;
                    }
                });
                localStorage.setItem('users', JSON.stringify(users));
                alert('修改成功')
            } else {
                alert('密码不一致')
            }
        } else {
            alert('密码不正确')
        }
    })
});